package org.fast.develop.logicflow.rest.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

/**
 * 统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class ControllerExceptionHandler {



    /**
     * 400 - Bad Request 参数绑定出错
     */
    // @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    public ResponseEntity<ErrorBaseVo> handleBindException(BindException e) {
        log.error("绑定参数出错", e);
        return new ResponseEntity<ErrorBaseVo>(
                // ErrorUtil.genErrorResult(HttpStatus.BAD_REQUEST),
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                HttpStatus.BAD_REQUEST);
    }

    /**
     * 400 - Bad Request
     */
    // @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<ErrorBaseVo> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("请求参数读取错误", e);
        return new ResponseEntity<ErrorBaseVo>(
                // ErrorUtil.genErrorResult(HttpStatus.BAD_REQUEST),
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                HttpStatus.BAD_REQUEST);
    }


    /**
     * 400 - Bad Request
     */
    // @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(NotFoundException.class)
    public ResponseEntity<ErrorBaseVo> handleNotFoundException(NotFoundException e) {
        log.error("请求参数读取错误", e);
        return new ResponseEntity<ErrorBaseVo>(
                HttpStatus.NOT_FOUND);
    }

    /**
     * 400 - Bad Request
     */
    // @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseEntity<ErrorBaseVo> handleValidationException(MethodArgumentNotValidException e) {
        log.error("请求参数验证失败", e);
        return new ResponseEntity<ErrorBaseVo>(
                // ErrorUtil.genErrorResult(HttpStatus.BAD_REQUEST),
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                HttpStatus.BAD_REQUEST);
    }

    /**
     * 405 - Method Not Allowed。HttpRequestMethodNotSupportedException
     * 是ServletException的子类,需要Servlet API支持
     */
    // @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<ErrorBaseVo> handleHttpRequestMethodNotSupportedException(
            HttpRequestMethodNotSupportedException e) {
        log.error("请求方法不支持", e);
        return new ResponseEntity<ErrorBaseVo>(
                // ErrorUtil.genErrorResult(HttpStatus.METHOD_NOT_ALLOWED),
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                HttpStatus.METHOD_NOT_ALLOWED);
    }

    /**
     * 415 - Unsupported Media Type。HttpMediaTypeNotSupportedException
     * 是ServletException的子类,需要Servlet API支持
     */
    // @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public ResponseEntity<ErrorBaseVo> handleHttpMediaTypeNotSupportedException(Exception e) {
        log.error("content-type类型不支持", e);
        return new ResponseEntity<ErrorBaseVo>(
                // ErrorUtil.genErrorResult(HttpStatus.UNSUPPORTED_MEDIA_TYPE),
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    /**
     * 500 - Internal Server Error
     */
    // @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorBaseVo> handleException(Exception e) {
        log.error("服务器内部错误", e);
        return new ResponseEntity<ErrorBaseVo>(
                // ErrorUtil.genErrorResult(HttpStatus.INTERNAL_SERVER_ERROR),
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 500 - Internal Server Error
     */
    // @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<ErrorBaseVo> handleException(RuntimeException e) {
        log.error("服务器内部错误", e);

        if (e instanceof ErrorInfoBase) {
            ErrorInfoBase errorInfoBase = (ErrorInfoBase) e;
            return new ResponseEntity<ErrorBaseVo>(ErrorBaseVo.builder().errorNo(errorInfoBase.getError_no())
                    .errorInfo(errorInfoBase.getError_info()).build(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<ErrorBaseVo>(
                ErrorBaseVo.builder().errorNo(ErrorInfos.CommonErr.ERROR.getError_no()).errorInfo(e.toString()).build(),
                // ErrorUtil.genErrorResult(HttpStatus.INTERNAL_SERVER_ERROR),
                HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(ErrInfoException.class)
    public ResponseEntity<ErrorBaseVo> UserNotFound(ErrInfoException e) {
        log.error("运行时出错", e);
        return new ResponseEntity((ErrorBaseVo.builder().errorNo("500").errorInfo(e.getError_info())), HttpStatus.INTERNAL_SERVER_ERROR);
    }


}
